package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.FactoryService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*

@RequestMapping("factories")
@RestController
class FactoryController(
    private val factoryService: FactoryService
) {

    data class QueryFactoriesRequest(
        val name: String?,
        val contact: String?,
        val address: String?,
        val disable:Boolean?,
    )

    @HasAuthority(Authority.货品_厂商_查询)
    @GetMapping(params = ["page"])
    fun queryFactories(pageable: Pageable, queryFactoryRequest: QueryFactoriesRequest): Page<QueryFactoriesResponse> {
        return this.factoryService.queryFactories(pageable, queryFactoryRequest)
    }

    data class QueryFactoriesResponse(
        val name: String,
        val id: Int,
        val goodsAmount: Int,
        val contact: String?,
        val address: String?,
        val disable:Boolean,
    )

    data class UpdateFactoryRequest(
        val name: String,
        val contact: String?,
        val address: String?
    )

    @HasAuthority(Authority.货品_厂商_修改)
    @PutMapping("{id}")
    fun updateFactory(@PathVariable id: Int, @RequestBody updateFactoryRequest: UpdateFactoryRequest) {
        this.factoryService.updateFactory(id, updateFactoryRequest)
    }

    @HasAuthority(Authority.货品_厂商_新增)
    @PostMapping
    fun createFactory(@RequestBody updateFactoryRequest: UpdateFactoryRequest): Int {
        return this.factoryService.createFactory(updateFactoryRequest).id
    }

    @DeleteMapping("{id}")
    fun deleteFactory(@PathVariable id: Int) {
        this.factoryService.deleteFactory(id)
    }

    @GetMapping
    fun getAllFactories(): List<IntIdAndName> {
        return this.factoryService.getAllFactories()
    }

    @PatchMapping("{id}/disable")
    fun disable(@PathVariable id: Int) {
        this.factoryService.disable(id)
    }

    @PatchMapping("{id}/enable")
    fun enable(@PathVariable id: Int) {
        this.factoryService.enable(id)
    }

}